// Sunghan Cho
// 301140084
// sunghanc@sfu.ca

#include "student.h"
#include <QStringList>
#include <QDebug>

Student::Student() : Item()
{
    m_name = "";
    m_stdNum = 0;
    m_email = "";
    m_preferredName = "";
}

Student::Student(QString name, int stdNum, QString email, QString prefName) : Item()
{
    m_name = name;
    m_stdNum = stdNum;
    m_email = email;
    m_preferredName = prefName;
}

int Student::getFieldCount()
{
    return 3;
}

QString Student::getFieldHeading(int fieldIndex)
{
    if(fieldIndex == 0){
        return "Name";
    }

    else if(fieldIndex == 1){
        return "Student #";
    }

    else if(fieldIndex == 2){
        return "Courses";
    }
    //qDebug()<<"Student::getFieldHeading index out of range";
    return 0;
}

QVariant Student::getFieldValue(int fieldIndex)
{
    if(fieldIndex == 0){
        return m_name;
    }
    else if(fieldIndex == 1){
        return m_stdNum;
    }
    else if(fieldIndex == 2){
        return getCoursesTakenNames();
    }
    //qDebug()<<"Student::getFieldValue index out of range";
    return 0;
}

bool Student::matchesPrimaryField(QVariant value)
{
    return value == m_stdNum ? true : false;
    /*if(value == m_stdNum){
        return true;
    }
    return false;*/
}

QString Student::getName()
{
    return m_name;
}

int Student::getStudentNumber()
{
    return m_stdNum;
}

QString Student::getEmail()
{
    return m_email;
}

QString Student::getPreferredName()
{
    return m_preferredName;
}

void Student::addCourse(Course *pCourse)
{
    m_courses.append(pCourse);
}

QString Student::getCoursesTakenNames()
{
    QString course1;
    QString course2;
    QStringList courseList;
    int count = 0;

    foreach(Course *itr, m_courses){
        courseList<<itr->getName();
        course1 = itr->getName();
        count++;
    }

    if(count == 1){
        return course1;
    }

    if(count > 1){
        course2 = courseList.join(", ");
        return course2;
    }

    return 0;
}

int Student::getCoursesTakenCount()
{
    return m_courses.count();
}

bool Student::hasTakenCourse(Course *pCourse)
{
    foreach(Course *itr, m_courses){
        if(itr == pCourse){
            return true;
        }
    }
    return false;
}
